package com.activity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class loginActivity extends Activity implements
		android.view.View.OnClickListener {
	EditText txtusername;
	EditText txtpassword;
	Button b1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainform);
		// TODO Auto-generated method stub
		txtusername = (EditText) findViewById(R.id.txtUserName);
		txtpassword = (EditText) findViewById(R.id.txtPassword);
		b1 = (Button) findViewById(R.id.btnLogin);
		b1.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (txtusername.getText().toString().equalsIgnoreCase("")) {
			Toast.makeText(this, "Please enter the user name",
					Toast.LENGTH_SHORT).show();
			return;
		} else if (txtusername.getText().toString().length() < 4) {
			Toast.makeText(this, "Invalid user name", Toast.LENGTH_SHORT)
					.show();
			return;
		} else if (txtpassword.getText().toString().equalsIgnoreCase("")) {
			Toast.makeText(this, "Please enter the password",
					Toast.LENGTH_SHORT).show();
			return;
		} else if (txtpassword.getText().toString().length() < 4) {
			Toast.makeText(this, "Invalid password", Toast.LENGTH_SHORT).show();
			return;
		} else {
			GetUser();
		}
	}

	private void GetUser() {
		Intent intent = getIntent();
		Uri data = intent.getData();
		if (data == null) {
			if (intent.hasExtra("home"))
				data = Uri.parse(intent.getExtras().getString("home"));
			else
				data = Uri
						.parse("http://10.10.2.114:8080/LeaveApplicationProcessingSystem/Login/*?android=true&username="
								+ txtusername.getText().toString()
								+ "&password="
								+ txtpassword.getText().toString());
		}
		StringBuilder b = new StringBuilder();
		try {
			HttpGet httpget = new HttpGet(new URI(data.toString()));
			HttpResponse response = new DefaultHttpClient().execute(httpget);
			InputStream inp = response.getEntity().getContent();
			BufferedReader rd = new BufferedReader(new InputStreamReader(inp));
			String line;
			while ((line = rd.readLine()) != null) {
				b.append(line);
			}
			if (b.toString().equalsIgnoreCase("Login Successful")) {
				Intent i = new Intent(this,
						AndroidLeaveApplicationActivity.class);
				startActivity(i);
			} else {
				Toast.makeText(this, b.toString(), Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			Toast.makeText(this, b.toString(), Toast.LENGTH_SHORT).show();
		}
	}
}
